package com.lry.basic.juc;

import sun.misc.Unsafe;

public class TestUnSafe {
    public static void main(String[] args) {


    }
}

class AtomicLong{

    private final Unsafe UNSAFE = Unsafe.getUnsafe();

    private volatile long value;

    private final long valueOffset;

    {
        try {
            valueOffset = UNSAFE.objectFieldOffset(AtomicLong.class.getDeclaredField("value"));
        } catch (NoSuchFieldException e) {
            throw new Error(e);
        }
    }

    final long incrementAndGet(){
        return UNSAFE.getAndAddLong(this, valueOffset, 1) + 1;
    }


    final boolean cas(long cmp,long val){
        //尝试把this对象的属性value从值cmp改为val
        return UNSAFE.compareAndSwapLong(this,valueOffset,cmp,val);
    }
}
